<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <el-row>
        <el-col :span="6"
          ><el-button type="primary">添加角色</el-button></el-col
        >
      </el-row>
      <el-table border style="width: 100%" :data="rolesList">
        <el-table-column type="expand" width="50">
          <template slot-scope="scope">
            <el-row
              v-for="(item1, index1) in scope.row.children"
              :key="item1.id"
              :class="['dbBottom', index1 == 0 ? 'bdTop' : '']"
            >
              <!-- 一级权限 -->
              <el-col :span="5"
                ><el-tag closable @close="removeRight(item3.id, scope.row)"
                  >{{ item1.authName }} </el-tag
                ><i class="el-icon-caret-right"></i
              ></el-col>
              <!-- 二三级权限 -->
              <el-col :span="19">
                <el-row
                  v-for="(item2, index2) in item1.children"
                  :key="item2.id"
                  :class="index2 == 0 ? '' : 'bdTop'"
                >
                  <el-col :span="6"
                    ><el-tag
                      type="success"
                      closable
                      @close="removeRight(item3.id, scope.row)"
                      >{{ item2.authName }} </el-tag
                    ><i class="el-icon-caret-right"></i
                  ></el-col>
                  <el-col :span="18" class="item3">
                    <el-tag
                      closable
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      type="warning"
                      class="items"
                      @close="removeRight(item3.id, scope.row)"
                      >{{ item3.authName }}</el-tag
                    >
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#" width="50"> </el-table-column>
        <el-table-column label="角色名称" prop="roleName"> </el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"
              >编辑</el-button
            >
            <el-button type="danger" icon="el-icon-delete" size="mini"
              >删除</el-button
            >
            <el-button
              type="warning"
              icon="el-icon-s-tools"
              size="mini"
              @click="getRightList(scope.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 弹出框 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="50%"
      @close="setDefauleKeysClear"
    >
      <el-tree
        ref="TreeRef"
        :data="rightList"
        :props="defaultProps"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="defauleKeys"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="allolRight">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from '@/axios/user'
export default {
  name: 'Roles',
  data() {
    return {
      rolesList: [],
      dialogVisible: false,
      rightList: [],
      defauleKeys: [], //默认选中的权限
      defaultProps: {
        children: 'children',
        label: 'authName',
      },
      roleId: '',
    }
  },
  methods: {
    //角色列表数据请求
    async getRolesList() {
      const { data } = await axios.get('/roles')
      //console.log(data)
      this.rolesList = data.data
    },
    //删除权限
    async removeRight(id, role) {
      console.log(role, id)
      const res = await this.$confirm(
        '此操作将永久删除该权限, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)
      console.log(res)
      if (res !== 'confirm') {
        return this.$message.info('取消删除')
      }
      const { data } = await axios.delete(`/roles/${role.id}/rights/${id}`)
      //console.log(data)
      if (data.meta.status !== 200) {
        return this.$message.error('删除失败')
      }
      role.children = data.data
    },
    //权限列表
    async getRightList(row) {
      this.roleId = row.id
      const { data } = await axios.get(`rights/tree`)
      //console.log(data)
      this.rightList = data.data
      //选出三级权限
      this.getLeavKey(row, this.defauleKeys)
      this.dialogVisible = true
    },
    getLeavKey(node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach((item) => {
        this.getLeavKey(item, arr)
      })
    },
    //
    setDefauleKeysClear() {
      this.defauleKeys = []
    },
    //权限授权
    async allolRight() {
      const keys = [
        ...this.$refs.TreeRef.getCheckedKeys(),
        ...this.$refs.TreeRef.getHalfCheckedKeys(),
      ].join(',')
      //console.log(keys)
      const { data } = await axios.post(`/roles/${this.roleId}/rights`, {
        rids: keys,
      })
      if(data.meta.status!==200){
       return  this.$message.error(data.meta.msg)
      }
      this.$message.success(data.meta.msg)
      this.getRolesList()
      this.dialogVisible = false
    },
  },
  created() {
    this.getRolesList()
  },
}
</script>

<style lang="scss" scoped>
.bdTop {
  border-top: 1px solid #eee;
}
.dbBottom {
  border-bottom: 1px solid #eee;
}
.el-row {
  display: flex;
  align-items: center;
}

.item3 {
  padding: 0 0 5px 0;
  .items {
    margin-right: 5px;
    margin-top: 5px;
  }
}
</style>